package com.mostsnails.common.base.utils.handler;


import com.mostsnails.common.base.constant.R;
import com.mostsnails.common.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

/**
 * <p>
 * 全局异常处理器
 * </p>
 *
 * @description: 在spring 3.2中，新增了@ControllerAdvice
 * 注解，可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute，并应用到所有@RequestMapping中
 * @author: MostSnails
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    // 参数校验异常处理 ===========================================================================
    // MethodArgumentNotValidException是springBoot中进行绑定参数校验时的异常,需要在springBoot中处理,其他需要处理ConstraintViolationException异常进行处理.

    /**
     * 方法参数校验
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("方法参数校验:" + e.getMessage(), e);
        return R.error(e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * ValidationException
     */
    @ExceptionHandler(ValidationException.class)
    public R<String> handleValidationException(ValidationException e) {
        Throwable cause = e.getCause();
        String message = cause.getMessage();
        log.error("ValidationException:", e);
        return R.error(message);
    }

    /**
     * ConstraintViolationException
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public R<String> handleConstraintViolationException(ConstraintViolationException e) {
        log.error("ValidationException:" + e.getMessage(), e);
        return R.error(e.getMessage());
    }

    @ExceptionHandler({BindException.class})
    public R<String> exception(BindException e) {
        log.error("BindException:", e);
        return R.error(e.getBindingResult().getFieldError().getDefaultMessage());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R<String> handlerNoFoundException(Exception e) {
        return R.error("路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R<String> handleDuplicateKeyException(DuplicateKeyException e) {
        return R.error("数据重复，请检查后提交:" + e.getMessage());
    }


    @ExceptionHandler(RuntimeException.class)
    public R<String> handleRuntimeException(RuntimeException e) {
        log.error("系统异常:", e);
        return R.error("系统异常，操作失败:" + e.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public R<String> handleRuntimeException(IllegalArgumentException e) {
        log.error("参数异常:", e);
        return R.error(e.getMessage());
    }


    @ExceptionHandler({Exception.class})
    public R<String> exception(Exception e) {
        log.error("其他错误:", e);
        return R.error("其他错误：" + e);
    }

    @ExceptionHandler({BusinessException.class})
    public R<String> exception(BusinessException e) {
        log.error("业务异常:", e);
        return R.error(e.getMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R<String> HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("请求参数非法:", e);
        return R.error("请求参数非法");
    }
}
